

/*
* Copyright @ 2017 com.iflysse.trains
* crm 上午1:20:33
* All right reserved.
*
*/
	
package com.qf.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qf.dto.CrmCourseDto;
import com.qf.dto.CrmFollowDto;
import com.qf.dto.CrmReferDto;
import com.qf.pojo.CrmClass;
import com.qf.pojo.CrmFollow;
import com.qf.pojo.CrmRefer;
import com.qf.pojo.CrmStaff;
import com.qf.pojo.CrmStudent;
import com.qf.pojo.PageCourse;
import com.qf.pojo.PageRefer;
import com.qf.service.CrmCourseTypeService;
import com.qf.service.CrmReferService;

/**
* @desc: crm
* @descripe:XXXX
* @author: 周锐
* @createTime: 2017年7月27日 上午1:20:33
* @history:
* @version: v1.0

*/
@Controller
public class CrmReferServlet {
	@Autowired
	@Qualifier("crmReferService")
   private CrmReferService referservice;
    @Autowired
    @Qualifier("crmCourseTypeService")
	private CrmCourseTypeService cts;
	
	/**
	* @param referservice the referservice to set
	*/
	
	public void setReferservice(CrmReferService referservice) {
	
		this.referservice = referservice;
	}
	
	
	/**
	* @param cts the cts to set
	*/
	
	public void setCts(CrmCourseTypeService cts) {
	
		this.cts = cts;
	}
	@RequestMapping("/findAllCourses.do")
	public ModelAndView findAllCourses(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		List<CrmCourseDto> allCourse = cts.findbyAllCourse();
		mv.addObject("allCourse", allCourse);
		mv.setViewName("/pages/refer/addRefer.jsp");
		return mv;	
		
	}
	@RequestMapping("/addRefer.do")
	public ModelAndView addRefer(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String stuName=request.getParameter("stuName");
		String telephone=request.getParameter("telephone");
		String qq=request.getParameter("qq");
		String intentionLevel=request.getParameter("intentionLevel");
		String courseTypeId=request.getParameter("courseTypeId");
		String classId=request.getParameter("classId");
		String source=request.getParameter("source");
		String remark = request.getParameter("remark");
		CrmStaff crmStaff=(CrmStaff)request.getSession().getAttribute("crmStaff");
		CrmRefer refer=new CrmRefer();
		refer.setStuName(stuName);
		refer.setTelephone(telephone);
		refer.setQq(qq);
		refer.setIntentionLevel(intentionLevel);
		refer.setCourseTypeId(Long.valueOf(courseTypeId));
		refer.setClassId(Long.valueOf(classId));
		refer.setSource(source);
		refer.setRemark(remark);
		refer.setCrmStaff(crmStaff);
		refer.setCreateDate(System.currentTimeMillis());
		refer.setStatus("咨询中");
		referservice.addRefer(refer);
		mv.setViewName("findReferbyPage.do");
		return mv;
		   
	}
	@RequestMapping("/findReferbyPage.do")
	public ModelAndView findReferbyPage(HttpServletRequest request,HttpServletResponse response){
		    ModelAndView mv=new ModelAndView();
	    	String page_=request.getParameter("page");
	    	int page=1;
	    	int size=3;
	    	if(page_!=null){
	    		page=Integer.parseInt(page_);
	    	}
	    	PageRefer pageRefer = referservice.findReferByPage(page, size);
	    	mv.addObject("pageRefer", pageRefer);
	    	mv.setViewName("/pages/refer/listRefer.jsp");
	    	return mv;
	
}
	@RequestMapping("/preupdateRefer.do")
	public ModelAndView preupdateRefer(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String referId = request.getParameter("referId");
		CrmReferDto crmReferDto = referservice.findReferById(Long.valueOf(referId));
		List<CrmCourseDto> allCourse = cts.findbyAllCourse();
		mv.addObject("allCourse", allCourse);
		mv.addObject("crmReferDto", crmReferDto);
		mv.setViewName("/pages/refer/editRefer.jsp");
		return mv;
}
	@RequestMapping("/updateRefer.do")
	public ModelAndView updateRefer(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String referId=request.getParameter("referId");
		String stuName=request.getParameter("stuName");
		String telephone=request.getParameter("telephone");
		String qq=request.getParameter("qq");
		String intentionLevel=request.getParameter("intentionLevel");
		String courseTypeId=request.getParameter("courseTypeId");
		String classId=request.getParameter("classId");
		String source=request.getParameter("source");
		String remark = request.getParameter("remark");
		CrmRefer refer = referservice.findReferuseId(Long.valueOf(referId));
		refer.setStuName(stuName);
		refer.setTelephone(telephone);
		refer.setQq(qq);
		refer.setIntentionLevel(intentionLevel);
		refer.setCourseTypeId(Long.valueOf(courseTypeId));
		refer.setClassId(Long.valueOf(classId));
		refer.setSource(source);
		refer.setRemark(remark);
		referservice.updateRefer(refer);
		mv.setViewName("findReferbyPage.do");
		return mv;
}
	@RequestMapping("/preaddfollow.do")
	public ModelAndView preaddfollow(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String referId=request.getParameter("referId");
		String staffId=request.getParameter("staffId");
		mv.addObject("referId", referId);
		mv.addObject("staffId", staffId);
		mv.setViewName("/pages/follow/addOrEditFollow.jsp");
		return mv;
		
	}
	@RequestMapping("/addfollow.do")
	public ModelAndView addfollow(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String referId=request.getParameter("referId");
		String staffId=request.getParameter("staffId");
		String content=request.getParameter("content");
		CrmRefer refer=new CrmRefer();
		refer.setReferId(Long.valueOf(referId));
		CrmStaff staff=new CrmStaff();
		staff.setStaffId(Long.valueOf(staffId));
		CrmFollow follow=new CrmFollow();
		follow.setCrmRefer(refer);
		follow.setCrmStaff(staff);
		follow.setContent(content);
		follow.setFollowTime(System.currentTimeMillis());
		referservice.addFollow(follow);
		mv.setViewName("findReferbyPage.do");
		return mv;
}
	@RequestMapping("/checkrefer.do")
	public ModelAndView checkrefer(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String referId = request.getParameter("referId");
		CrmReferDto crmReferDto = referservice.findReferById(Long.valueOf(referId));
		List<CrmFollowDto> follows = referservice.findfollowbyrefer(Long.valueOf(referId));
		mv.addObject("crmReferDto", crmReferDto);
		mv.addObject("follows", follows);
		mv.setViewName("/pages/refer/showRefer.jsp");
		return mv;
		
	}
	@RequestMapping("/preaddStudent.do")
	public ModelAndView preaddStudent(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String referId = request.getParameter("referId");
		CrmReferDto crmReferDto = referservice.findReferById(Long.valueOf(referId));
		mv.addObject("crmReferDto", crmReferDto);
		mv.setViewName("/pages/refer/addStudent.jsp");
		return mv;
}
	@RequestMapping("/addStudent.do")
	public ModelAndView addStudent(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		String classId = request.getParameter("classId");
		String referId = request.getParameter("referId");
		String stuName=request.getParameter("stuName");
		String telephone=request.getParameter("telephone");
		String qq = request.getParameter("qq");
		String identity=request.getParameter("identity");
		CrmClass crmClass=new CrmClass();
		crmClass.setClassId(Long.valueOf(classId));
		CrmRefer refer=new CrmRefer();
		refer.setReferId(Long.valueOf(referId));
		CrmStudent student=new CrmStudent();
		student.setCrmClass(crmClass);
		student.setCrmRefer(refer);
		student.setStuName(stuName);
		student.setTelephone(telephone);
		student.setQq(qq);
		student.setIdentity(identity);
		referservice.addCrmStudent(student);
		CrmRefer crmRefer = referservice.findReferuseId(Long.valueOf(referId));
		crmRefer.setStatus("已报名");
		referservice.updateRefer(crmRefer);
		mv.setViewName("findReferbyPage.do");
		return mv;
}
}